package com.cy.ppj.web.admin.rule;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.manager.ManagerAddAO;
import com.cy.ppj.model.ao.manager.ManagerEditAO;
import com.cy.ppj.model.ao.manager.ManagerUpdatePasswordAO;
import com.cy.ppj.model.ao.rule.AuthorizationAO;
import com.cy.ppj.model.ao.rule.RoleAddAO;
import com.cy.ppj.model.ao.rule.RoleEditAO;
import com.cy.ppj.model.domian.admin.Manager;
import com.cy.ppj.model.domian.rule.Role;
import com.cy.ppj.model.enums.ManagerStatus;
import com.cy.ppj.model.enums.ManagerType;
import com.cy.ppj.model.query.ManagerAdminQuery;
import com.cy.ppj.model.query.rule.RoleAdminQuery;
import com.cy.ppj.model.vo.ManagerAdminVO;
import com.cy.ppj.model.vo.admin.rule.ManagerTypeVO;
import com.cy.ppj.model.vo.admin.rule.MenuAdminVO;
import com.cy.ppj.service.manager.ManagerService;
import com.cy.ppj.service.rule.RuleService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 后台权限控制
 * @author: Ocean
 * @create: 2018-07-31 17:34
 **/
@Slf4j
@RestController
@RequestMapping("/admin/rule")
@Api(tags = "后台-权限管理")
public class AdminRuleController extends BaseController {

    @Autowired
    private RuleService ruleService;
    @Autowired
    private ManagerService managerService;

    @ApiOperation("新增角色")
    @PostMapping("/role/add")
    public Result roleAdd(@Validated RoleAddAO roleAddAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Role role = new Role();
        BeanUtils.copyProperties(roleAddAO, role);
        ruleService.addRole(role);

        return ok();
    }

    @ApiOperation("编辑角色")
    @PostMapping("/role/edit")
    public Result roleEdit(@Validated RoleEditAO roleEditAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Role role = new Role();
        BeanUtils.copyProperties(roleEditAO, role);
        ruleService.updateRole(role);

        return ok();
    }

    @ApiOperation("管理员列表")
    @GetMapping("/manager/list")
    public PageBean<ManagerAdminVO> managerList(ManagerAdminQuery query) {
    	PageBean<ManagerAdminVO> pageBean = managerService.selectManagerAdminList(query); 
        return pageBean;
    }

    @ApiOperation("添加管理员")
    @PostMapping("/manager/add")
    public Result managerAdd(@Validated ManagerAddAO managerAddAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerAddAO, manager);
        managerService.addManger(manager);
        return ok();
    }

    @ApiOperation("编辑管理员")
    @PostMapping("/manager/edit")
    public Result managerEdit(@Validated ManagerEditAO managerEditAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerEditAO, manager);
        if(ManagerType.GENERAL_MANAGER.equals(manager.getType())) {
        	manager.setCityId(0L);
        	manager.setCompanyId(0L);
        }else if(ManagerType.CITY_LEADER.equals(manager.getType())) {
        	manager.setCompanyId(0L);
        }else if(ManagerType.COMPANY_LEADER.equals(manager.getType())) {
        	manager.setCityId(0L);
        }
        managerService.updateManager(manager);
        return ok();
    }

    @ApiOperation("管理员更改状态")
    @PostMapping("/manager/update/status")
    public Result updateStatus(@ApiParam(value = "管理员ID", example = "0", required = true) @RequestParam Long managerId,
                               @ApiParam(value = "状态", required = true) @RequestParam ManagerStatus status) {
        managerService.updateStatus(managerId, status);
        return ok();
    }

    @ApiOperation("管理员权限树状图")
    @GetMapping("/tree")
    public Result tree(Long roleId) {

        List<MenuAdminVO> tree = ruleService.queryMenusAdminListInCache(roleId);

        return ok(null, tree);
    }

    @ApiOperation("角色列表，分页")
    @GetMapping("/role/list")
    public PageBean<Role> roleList(RoleAdminQuery query) {
        return ruleService.queryRoleAdminList(query);
    }

    @ApiOperation("所有角色列表，不分页")
    @GetMapping("/all/roles")
    public Result allRolesInCache() {
    	return ok("", ruleService.allRolesInCache());
    }
    
    @ApiOperation("管理员重置密码")
    @PostMapping("/manager/reset/pwd")
    public Result resetManagerPwd(@ApiParam(value = "管理员ID", example = "0", required = true) @RequestParam Long managerId) {
    	managerService.resetManagerPwd(managerId);
    	return ok();
    }

    @ApiOperation("添加权限")
    @PostMapping("/authorization")
    public Result authorization(@Validated AuthorizationAO authorizationAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        ruleService.addRoleMenu(authorizationAO.getRoleId(), authorizationAO.getMenuIds());

        return ok();
    }
    
    @ApiOperation("管理员修改密码")
    @PostMapping("/manager/update/pwd")
    public Result resetManagerPwd(@Validated ManagerUpdatePasswordAO ao, BindingResult bindingResult) {
    	this.checkError(bindingResult);
    	managerService.updatePassword(this.getManager().getManagerId(), ao.getOldPassword(), ao.getNewPassword());
    	return ok();
    }
    
    @ApiOperation("管理员账户类型列表")
    @GetMapping("/manager/all/types")
    public Result<ManagerTypeVO> managetTypes() {
    	List<ManagerTypeVO> list = ManagerType.getList();
    	return ok("", list);
    }
    
}